package com.ruix.app.widget;


import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.cache.CacheFactory;
import com.ruix.app.mapper.LessonPostMemberMapper;
import com.ruix.app.model.LessonPostMember;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.adapter.LessonSumupMembersAdapter;
import com.ruix.app.ui.stratum.lesson.ReleaseLearningFeelActivity;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/5/23.
 */

public class LessonSumupView extends LinearLayout {

    private TextView sumupContentView, writeButton;
    private TextView sumupNumberView;
    private GridView sumupPersonGrid;
    private String leesonID;
    private ArrayList<LessonPostMember> members;
//    private LessonSumupMemberAdapter adapter;

    private LessonSumupMembersAdapter adapters;

    public LessonSumupView(Context context) {
        super(context);
        initView(context);
    }

    public LessonSumupView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public LessonSumupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(final Context context) {
        LinearLayout.inflate(context, R.layout.item_sumup_view, this);
        sumupContentView = (TextView) findViewById(R.id.tv_lesson_sumup_content);
        writeButton = (TextView) findViewById(R.id.tv_write_something);
        sumupNumberView = (TextView) findViewById(R.id.tv_persion_number);
        sumupPersonGrid = (GridView) findViewById(R.id.gv_sumup_persion_ava);
        adapters = new LessonSumupMembersAdapter(context);
        sumupPersonGrid.setAdapter(adapters);
        writeButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //ReleaseLearningFeelActivity
//                Intent intent =  ReleaseLearningFeelActivity.newIntent(context,leesonID);
//                ((Activity)context).startActivity(intent);
            }
        });
        setSumupNumber(0);
    }

    public void setLeesonID(String leesonID) {
        this.leesonID = leesonID;
    }

    public void setContent(String content) {
        sumupContentView.setText(content);
    }

    private void setSumupNumber(int number) {
        String numberStr = String.valueOf(number);
        String value = String.format("%s人已写", numberStr);
        SpannableString ss = new SpannableString(value);
        ss.setSpan(new ForegroundColorSpan(0xFFFF6565), 0, numberStr.length(),
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        sumupNumberView.setText(ss);
    }


    public void setMembers(ArrayList<LessonPostMember> members) {
        this.members = members;
        if (members != null && members.size() > 0){
            setSumupNumber(members.size());
            adapters.setMembers(members);
        } else {
            setSumupNumber(0);
        }
    }

    public void updateSubmitMenbers(){
        loadCache();
        loadPostMember();
    }

    private void loadPostMember() {
        RequestFactory.getCoursePostMember(leesonID, new LessonPostMemberMapper(), new RXObserver<ArrayList<LessonPostMember>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ArrayList<LessonPostMember> value) {
                setMembers(value);
                CacheFactory.cacheExpireMenberList(leesonID,value);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void loadCache(){
        CacheFactory.loadExpireMenberList(leesonID,new RXObserver<ArrayList<LessonPostMember>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ArrayList<LessonPostMember> value) {
                setMembers(value);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

}
